# -*- coding: utf-8 -*-
"""
Coroutines and concurrency/Tornado Queues/test04.py
"""
import asyncio
import random
from tornado import queues
from tornado import gen

q = queues.PriorityQueue(maxsize=5)


async def consumer():
    """
    消费者
    :return:
    """
    async for item in q:
        try:
            print(f'Current queue size: {q.qsize()}')
            print(f'Doing work on {item}')
            await gen.sleep(0.01)
        finally:
            q.task_done()


async def producer():
    """
    生产者
    :return:
    """
    for item in range(10):
        item_id = random.randint(1, 10)
        obj_item = (item_id, f'Item priority id is {item_id}')
        await q.put(obj_item)
        print(f'Put {obj_item}')


async def main():
    task1 = asyncio.create_task(consumer())
    await producer()
    await task1


if __name__ == '__main__':
    asyncio.run(main())
